package us.icitap.cm.ui.policecase;

import java.util.Arrays;

import us.icitap.cm.CaseManagementUI;
import us.icitap.cm.business.AppListTypeEnum;
import us.icitap.cm.business.entities.AppEntity;
import us.icitap.cm.business.entities.AppList;
import us.icitap.cm.business.entities.Control;
import us.icitap.cm.business.entities.Control.ControlRequestEnum;
import us.icitap.cm.business.entities.EntityTypeEnum;
import us.icitap.cm.business.entities.Report;
import us.icitap.cm.listeners.AppEntityListener;
import us.icitap.cm.ui.custom.CustBeanFieldGroup;
import us.icitap.cm.ui.custom.CustComboBox;
import us.icitap.cm.ui.custom.CustDateField;
import us.icitap.cm.ui.custom.CustTextField;
import us.icitap.cm.ui.custom.CustomFormLayout;
import us.icitap.cm.ui.shared.AppEntityLayout;
import us.icitap.cm.utils.ProjectLists;
import us.icitap.utils.FormatingUtils;

import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.fieldgroup.FieldGroup.CommitException;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.shared.ui.datefield.Resolution;
import com.vaadin.ui.AbstractSelect.ItemCaptionMode;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.TabSheet;

public class ControlDataLayout extends AppEntityLayout{

	private Control control;
	private final CustBeanFieldGroup<Control> binder = new CustBeanFieldGroup<Control>(Control.class);
	
	private CustDateField fromDate = new CustDateField();
	private CustDateField toDate = new CustDateField("deri: ");
	
	private final CustComboBox entitySelect = new CustComboBox("Entiteti");
	private final CheckBox courtDecisionCheckBox = new CheckBox("Me vendim gjykate");
	private CustTextField courtDecisionNoTextField = new CustTextField("Nr. i vendimit te gjykates");
	private CustDateField courtDecisionDateField = new CustDateField("Data e vendimit");
	
	private final CheckBox officerIniciativeCheckBox = new CheckBox("Me iniciative te oficerit te policise gjygjesore");
	private final CustComboBox controlReasonSelect = new CustComboBox("Qellimi i kontrollit");
	
	private CustComboBox controlRequestSelect = new CustComboBox("Kerkesa per kontroll");
	
	
	final CustomFormLayout cfl = new CustomFormLayout("550px", "Dokumenta te perfshire ne ngjarje", "Reset", "Ruaj");
	
	final TabSheet tabSheet = new TabSheet();
	
	private final BeanItemContainer<AppEntity> appEntityContainer = new BeanItemContainer<AppEntity>(AppEntity.class);
	
	private ControlAppEntityFoundDataLayout controlAppEntityFoundDataLayout = new ControlAppEntityFoundDataLayout("500px");
	
	
	protected AppEntityListener listener;
	private Report report;
	
	
	
	public ControlDataLayout() {
		this.setMargin(true);

		this.fromDate.setDateFormat(FormatingUtils.DATE_TIME_FORMAT);
		this.fromDate.setResolution(Resolution.MINUTE);
		this.toDate.setDateFormat(FormatingUtils.TIME_FORMAT);
		this.toDate.setResolution(Resolution.MINUTE);
		this.courtDecisionDateField.setDateFormat(FormatingUtils.SHORT_DATE_FORMAT);
		
		HorizontalLayout hl = new HorizontalLayout();
		hl.setSpacing(true);
		hl.setCaption("Intervali kohor: ");
		hl.addComponent(fromDate);
		hl.setComponentAlignment(fromDate, Alignment.BOTTOM_LEFT);
		FormLayout toDateFl = new FormLayout();
		toDateFl.setMargin(false);
		toDateFl.addComponent(toDate);
		hl.addComponent(toDateFl);
		
		FormLayout fl = new FormLayout();
		fl.addStyleName("cmLayout");
		fl.setMargin(true);
		
		fl.addComponent(hl);
		
		fl.addComponent(this.entitySelect);
		fl.addComponent(this.courtDecisionCheckBox);
		fl.addComponent(this.courtDecisionNoTextField);
		fl.addComponent(this.courtDecisionDateField);
		fl.addComponent(this.officerIniciativeCheckBox);
		fl.addComponent(this.controlReasonSelect);
		fl.addComponent(this.controlRequestSelect);
		
		
		
		tabSheet.addTab(fl, "Te dhenat");
		tabSheet.addTab(controlAppEntityFoundDataLayout, "Objektet/Personat e gjetur");
		
		cfl.addComponent(tabSheet);
		cfl.addFooter();
		
	
		entitySelect.setNullSelectionAllowed(false);
		entitySelect.setContainerDataSource(appEntityContainer);
		entitySelect.setItemCaptionMode(ItemCaptionMode.EXPLICIT);
		entitySelect.setItemCaptionPropertyId("alternativeString");
		
		controlReasonSelect.setContainerDataSource(new BeanItemContainer<AppList>(AppList.class, ProjectLists.getList(AppListTypeEnum.ControlReason, false)));
		
		
		controlRequestSelect.setContainerDataSource(new BeanItemContainer<ControlRequestEnum>(ControlRequestEnum.class, Arrays.asList(ControlRequestEnum.values())));
		controlRequestSelect.setItemCaptionMode(ItemCaptionMode.EXPLICIT);
		controlRequestSelect.setItemCaptionPropertyId("caption");
		
		isCourtDecision(false);
		isOfficerIniciative(false);
		this.courtDecisionCheckBox.setImmediate(true);
		this.courtDecisionCheckBox.addValueChangeListener(new ValueChangeListener() {
			
			@Override
			public void valueChange(ValueChangeEvent event) {
				isCourtDecision(courtDecisionCheckBox.getValue().booleanValue());		
			}
		});
		
		this.officerIniciativeCheckBox.setImmediate(true);
		this.officerIniciativeCheckBox.addValueChangeListener(new ValueChangeListener() {
			
			@Override
			public void valueChange(ValueChangeEvent event) {
				isOfficerIniciative(officerIniciativeCheckBox.getValue().booleanValue());		
			}
		});
		
		
		cfl.addRightClickListener(new ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				if(ControlDataLayout.this.listener != null){
					try {
						binder.commit();
						
						
						if(control.getAppEntity().isNewRecord())
							ControlDataLayout.this.listener.addAppEntityToReport(control.getAppEntity());
						else if(!oldAppEntity.getControl().myEquals(control)){
							ControlDataLayout.this.listener.updateAppEntityIntoReport(oldAppEntity, control.getAppEntity());
						}
							
							
						
					} catch (CommitException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}
				
			}
		});
		
		this.addComponent(cfl);
		
		binder.bind(this.fromDate, "startDate");
		binder.bind(this.toDate, "endDate" );
		binder.bind(this.courtDecisionCheckBox, "courtDecision" );
		binder.bind(this.courtDecisionNoTextField, "courtDecisionNo");
		binder.bind(this.courtDecisionDateField, "courtDecisionDate");
		binder.bind(this.officerIniciativeCheckBox, "officerIniciative");
		binder.bind(this.controlReasonSelect, "controlReason");
		binder.bind(this.controlRequestSelect, "controlRequestEnum");
		binder.bind(this.entitySelect, "subjectAppEntity");
		
	}
	
	
	private void isCourtDecision(boolean courtDecision){
		if(!courtDecision){
			this.courtDecisionNoTextField.setValue(null);
			this.courtDecisionDateField.setValue(null);	
		}
		this.courtDecisionNoTextField.setVisible(courtDecision);
		this.courtDecisionDateField.setVisible(courtDecision);
		
	}
	
	private void isOfficerIniciative(boolean officerIniciative){
		if(!officerIniciative)
			controlReasonSelect.setValue(null);
		
		this.controlReasonSelect.setVisible(officerIniciative);
	}
	

	@Override
	public void addAppEntity() {
		
		AppEntity appEntity = new AppEntity(CaseManagementUI.getUser().getUsername(), EntityTypeEnum.CONTROL);
		appEntity.getControl().setReport(report);
		this.control = appEntity.getControl();
		
		this.binder.setItemDataSource(control);
		
		this.loadEntityList(report);
		controlAppEntityFoundDataLayout.setControl(this.control, true);
	}

	private AppEntity oldAppEntity;
	
	@Override
	public void loadAppEntity(AppEntity appEntity, boolean editable) {
		
		this.oldAppEntity = appEntity;
		AppEntity newAppEntity = new AppEntity(appEntity);
		
		this.control = newAppEntity.getControl();

		
		newAppEntity.setNewRecord(false);
//		this.control = appEntity.getControl();
		
		this.binder.setItemDataSource(control);
		
		this.loadEntityList(report);
		System.out.println(report);
		controlAppEntityFoundDataLayout.setControl(this.control, true);
	}


	private void loadEntityList(Report report){
		if(report != null && report.getEntityList() != null && !report.getEntityList().isEmpty()){
			for (AppEntity appEntity : report.getEntityList()) {
				if(appEntity.getEntityType().equals(EntityTypeEnum.PLACE)
					|| appEntity.getEntityType().equals(EntityTypeEnum.VEHICLE)
					|| appEntity.getEntityType().equals(EntityTypeEnum.PERSON)){
					appEntityContainer.addBean(appEntity);
				}
			}
		}
	}
	
	
	@Override
	public void addAppEntityListener(AppEntityListener listener) {
		this.listener = listener;
		
	}
	
	
	public void setReport(Report report){
		this.report = report;
		
	}
	
}
